<?php
/**
 * Created by PhpStorm.
 * User: haobin
 * Date: 2016/10/18
 * Time: 9:02
 */
//徒弟列表
require('../conn.php');
require('../functions.php');
require('admin.php');
$page = $_GET['page'];
if (is_numeric($page) == false) {
    $page = 1;
    $page_1 = 1;
    $min = 0;
    $page_2 = $page + 1;
} else {
    if ($page > 1) {
        $page_2 = $page + 1;
        $min = ($page - 1) * 35;
        $page_1 = $page - 1;
    } else {
        $page_1 = 1;
        $min = 0;
        $page_2 = 2;
    }
}
$max = $page * 35;
//获取师傅id
$pid = $_GET['id'];
$pinfo = $mysql->query("select * from `user` WHERE `id`={$pid}");
$pname = $pinfo[0]['name'];
$sql = "select * from `user` WHERE `pid`={$pid} order by `id` desc limit {$min},{$max}";
//永久封号,ban置为-1
$del_id = guolv($_GET['del_id']);
if (is_numeric($del_id)) {
    $mysql->query("update `user` set `ban` = -1  where `id`={$del_id}");
    echo "<script>alert('封号成功');location.href='students.php?id={$pid}'</script>";
    exit;
}
//禁言
if ($_GET['action'] == 'ban') {
    $uid = $_GET['uid'];
    $day = date("Y-m-d", time());
    $res = $mysql->execute("update `user` set `ban` = 1,`ban_day` = '{$day}' WHERE `id` = {$uid}");
    if ($res) {
        echo "<script>alert('禁言成功');location.href='students.php?id={$pid}'</script>";
    }
}
if ($_GET['do'] == 'search') {
    $phone = guolv($_GET['phone']);
    if (substr($phone, 0, 1) == '#') {
        $search_id = str_replace('#', '', $phone);
        $sql = "select * from `user` where `id`='{$search_id}' order by `id` desc limit {$min},{$max}";
    } else if (substr($phone, 0, 1) == '@') {
        $search_id = str_replace('@', '', $phone);
        $sql = "select * from `user` where `phone`='{$search_id}' order by `id` desc limit {$min},{$max}";
    } else {
        $sql = "select * from `user` where `name` LIKE '%{$phone}%' order by `id` desc limit {$min},{$max}";
    }
}
if ($_GET['do'] == 'money') {
    $sql = "select * from `user` WHERE `pid`={$pid} order by `total` desc limit {$min},{$max}";
}
//批量封号用户
if ($_POST) {
    $userArr = $_POST['del_id'];
    foreach ($userArr as $id) {
        $res = $mysql->execute("update `user` set `ban` = -1  where `id`={$id}");
    }
    if ($res) {
        echo "<script>alert('操作成功');location.href='students.php?id={$pid}'</script>";
    }
}
$row_user = $mysql->query($sql);
?>

<?php include('head.php') ?>
<div class="container-fluid">
    <div class="row-fluid">
        <?php
        include('left.php'); ?>
        <div class="span9">
            <h1 class="page-title"><span style="color:red">(<?= $pname ?>)</span>徒弟列表</h1>

            <div class="well">
                <div id="myTabContent" class="tab-content">
                    <div class="tab-pane active in" id="home">
                        <div class="well">
                            <form class="navbar-search pull-left" action="user.php" method="get">
                                <input type="hidden" name="do" value="search">
                                <input type="text" class="search-query" placeholder="#+id搜索会员" name="phone">
                            </form>
                            <div class="pull-right">
                                <a href="students.php?id=<?= $pid ?>" class="btn">首页</a></button>
                                <a href="students.php?<?php if ($_GET['do'] == 'money') {
                                    echo "do=money&";
                                } ?>page=<?= $page_1 ?>&id=<?= $pid ?>" class="btn"
                                   style="margin-left:5px;">上一页</a>
                                <a href="students.php?<?php if ($_GET['do'] == 'money') {
                                    echo "do=money&";
                                } ?>page=<?= $page_2 ?>&id=<?= $pid ?>" class="btn"
                                   style="margin-left:5px;">下一页</a>
                            </div>
                            <form method="post">
                                <table class="table">
                                    <thead>
                                    <tr>
                                        <th></th>
                                        <th>#</th>
                                        <th>手机号</th>
                                        <th>昵称</th>
                                        <th>注册时间</th>
                                        <th><a href="?do=money&id=<?= $pid ?>">余积分</a></th>
                                        <th>余金币</th>
                                        <th>已提现</th>
                                        <th>师傅ID</th>
                                        <th>徒弟数</a></th>
                                        <th>状态</th>
                                        <th>操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <?php
                                    foreach ($row_user as $user) {
                                        if ($user['ban'] == 0) {
                                            $ban = "<font color='green'>正常</font>";
                                        } else if ($user['ban'] == 1) {
                                            $ban = "<font color='blue'>禁言</font>";
                                        } else if ($user['ban'] == -1) {
                                            $ban = "<font color='red'>封号</font>";
                                        }
                                        $studentNum = $mysql->query("select count(*) as num from `user` WHERE `pid`={$user['id']} AND login = 1");
                                        $txmoney = $mysql->query("select sum(money) as money from `txdata` WHERE `uid`={$user['id']} AND `state`=2");
                                        print <<<table
        <tr>
          <td><input type="checkbox" name="del_id[]" value="{$user[id]}"></td>
          <td><a target="_blank"  href="http://app.weiyouqu.net/weiApp/user_detail.php?id={$user['id']}" title="查看用户明细">{$user['id']}</a></td>
     	  <td>{$user['phone']}</td>
     	  <td>{$user['name']}</td>
     	  <td>{$user['day']}</td>
		  <td>{$user['total']}</td>
		  <td>{$user['gold']}</td>
		  <td>{$txmoney[0]['money']}</td>
		  <td>{$user['pid']}</td>
		   <td><a target="_blank" href="./students.php?id={$user['id']}" title="徒弟列表">{$studentNum[0]['num']}</a></td>
		  <td>{$ban}</td>
		  <td>
			<a class="btn" style="color:red" title="封号" onclick="del_id({$user['id']})">封号</a>
			<a class="btn" style="color:green" title="编辑" href="javascript:if(confirm('确认禁言？')){window.location.href = './user.php?uid={$user['id']}&action=ban';}">禁言</a>
			<a class="btn" target="_blank"style="color:green" title="编辑" href="./edit_user.php?uid={$user['id']}">编辑</a>
		  </td>
        </tr>
table;
                                    } ?>
                                    </tbody>
                                </table>
                                <input type="submit" value="批量封号">
                            </form>
                        </div>
                    </div>
                    <div class="tab-pane fade" id="profile">
                    </div>
                </div>

            </div>

        </div>
    </div>


</div>

<footer>
    <hr>

    <p class="pull-right"><!--power by right--></p>


    <p><!--power by --></p>
</footer>


<!-- Le javascript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="lib/bootstrap/js/bootstrap.js"></script>
<script language="JavaScript">
    function del_id(id) {
        if (confirm("确认封号？")) {
            location.href = "?del_id=" + id;
        } else {
            return false;
        }
    }
</script>


</body>
</html>